package com.gxuwz.prrs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.gxuwz.prrs.R;
import com.gxuwz.prrs.bean.AppointmentRecord;
import com.gxuwz.prrs.constant.Constants;
import com.gxuwz.prrs.service.ServiceHolder;
import com.gxuwz.prrs.service.impl.AppointmentRecordServiceImpl;
import com.gxuwz.prrs.utils.NavigationUtils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author 张钧
 * @Description
 * @create 2023-05-27 下午 08:28
 */
public class AppointmentRecordAdapter extends UpdatableAbstractAdapter<AppointmentRecord> {

	private final List<AppointmentRecord> appointmentRecords;
	private final LayoutInflater inflater;

	private final static AppointmentRecordServiceImpl APPOINTMENT_RECORD_SERVICE = ServiceHolder.getInstance().getBean(AppointmentRecordServiceImpl.class);

	public AppointmentRecordAdapter(Context context, List<AppointmentRecord> appointmentRecords) {
		this.appointmentRecords = appointmentRecords;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return appointmentRecords.size();
	}

	@Override
	public AppointmentRecord getItem(int i) {
		return appointmentRecords.get(i);
	}

	@Override
	public long getItemId(int i) {
		return appointmentRecords.get(i).getAppointmentRecordId();
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		AppointmentRecordViewHolder viewHolder;
		if (view == null) {
			view = inflater.inflate(R.layout.appointment_item_layout, viewGroup, false);
			viewHolder = new AppointmentRecordViewHolder();
			viewHolder.setPianoRoomNo(view.findViewById(R.id.piano_room_no));
			viewHolder.setAppointmentDate(view.findViewById(R.id.appointment_date));
			viewHolder.setStatus(view.findViewById(R.id.status));
			viewHolder.setAppointmentTime(view.findViewById(R.id.appointment_time));
			viewHolder.setCancelAppointment(view.findViewById(R.id.cancel_appointment));
			view.setTag(viewHolder);
		} else {
			viewHolder = (AppointmentRecordViewHolder) view.getTag();
		}
		AppointmentRecord itemData = getItem(i);
		String appointmentDateStr = itemData.getAppointmentDate();
		String appointmentTimeStr = itemData.getAppointmentTime();
		viewHolder.getPianoRoomNo().setText(String.valueOf(itemData.getPianoRoomNo()));
		viewHolder.getAppointmentDate().setText(appointmentDateStr);
		viewHolder.getStatus().setText(itemData.getStatus());
		viewHolder.getAppointmentTime().setText(appointmentTimeStr);
		Button cancelAppointment = viewHolder.getCancelAppointment();
		String[] times = appointmentTimeStr.split("-");
		LocalDate appointmentDate = LocalDate.parse(appointmentDateStr);
		LocalTime appointmentTime = LocalTime.parse(times[0], DateTimeFormatter.ofPattern(Constants.TIME_PATTERN));
		appointmentTime = appointmentTime.minusMinutes(30);
		cancelAppointment.setVisibility(View.VISIBLE);
		cancelAppointment.setClickable(true);
		cancelAppointment.setEnabled(true);
		LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
		now = now.plusHours(8);
		LocalDate localDate = now.toLocalDate();
		LocalTime localTime = now.toLocalTime();
		/*
		 状态为已预约
		"已预约".equals(itemData.getStatus())
		&&
		(
		当前日期早于预约日期
		localDate.isBefore(appointmentDate)
		||
		当前日期早于或等于预约日期且练琴时间减去30分钟依旧晚于当前时间
		(
		当前日期不晚于预约日期
		!localDate.isAfter(appointmentDate)
		&&
		预约时间减去30分钟后晚于当前时间
		appointmentTime.isAfter(localTime))
		)
		 */
		if ("已预约".equals(itemData.getStatus()) &&
				(localDate.isBefore(appointmentDate) ||
						(!localDate.isAfter(appointmentDate)
								&& appointmentTime.isAfter(localTime)))) {
			cancelAppointment.setOnClickListener(view1 -> {
				AppointmentRecord appointmentRecord = new AppointmentRecord();
				appointmentRecord.setAppointmentRecordId(itemData.getAppointmentRecordId());
				appointmentRecord.setStatus("已取消");
				Boolean b = APPOINTMENT_RECORD_SERVICE.updateAppointment(appointmentRecord);
				if (b) {
					Toast.makeText(view1.getContext(), "取消成功", Toast.LENGTH_SHORT).show();
					NavigationUtils.redirectToAppointmentRecordsActivity(view1.getContext(), null);
				} else {
					Toast.makeText(view1.getContext(), "取消失败，请重试", Toast.LENGTH_SHORT).show();
					NavigationUtils.redirectToAppointmentRecordsActivity(view1.getContext(), null);
				}
			});
		} else {
			cancelAppointment.setVisibility(View.INVISIBLE);
			cancelAppointment.setClickable(false);
			cancelAppointment.setEnabled(false);
		}
		return view;
	}

	@Override
	public void updateData(List<AppointmentRecord> newData) {
		if (newData != null) {
			appointmentRecords.clear();
			appointmentRecords.addAll(newData);
			notifyDataSetChanged();
		}
	}
}

class AppointmentRecordViewHolder {
	private TextView pianoRoomNo;
	private TextView appointmentDate;
	private TextView status;
	private TextView appointmentTime;
	private Button cancelAppointment;

	public TextView getPianoRoomNo() {
		return pianoRoomNo;
	}

	public void setPianoRoomNo(TextView pianoRoomNo) {
		this.pianoRoomNo = pianoRoomNo;
	}

	public TextView getAppointmentDate() {
		return appointmentDate;
	}

	public void setAppointmentDate(TextView appointmentDate) {
		this.appointmentDate = appointmentDate;
	}

	public TextView getStatus() {
		return status;
	}

	public void setStatus(TextView status) {
		this.status = status;
	}

	public TextView getAppointmentTime() {
		return appointmentTime;
	}

	public void setAppointmentTime(TextView appointmentTime) {
		this.appointmentTime = appointmentTime;
	}

	public Button getCancelAppointment() {
		return cancelAppointment;
	}

	public void setCancelAppointment(Button cancelAppointment) {
		this.cancelAppointment = cancelAppointment;
	}
}
